package com.dao;

import com.entity.Comment;
import java.util.List;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.ResultMap;


/**
 * 评论实体的 MyBatis 映射接口。
 * 方法应对应于 CommentMapper.xml 中的 SQL 语句或使用 MyBatis 注解。
 */

@Mapper
public interface CommentDao {

    /**
     * 根据评论 ID 查询评论。
     */
    @Select("SELECT c.id AS comment_id, c.created_at AS comment_create_time, c.updated_at AS comment_update_time, " +
            "c.content AS comment_content, c.product_id AS goods_id, c.parent_comment_id AS parent_id, " +
            "s.id AS student_id, s.username AS student_name, " +
            "ps.id AS parent_student_id, ps.username AS parent_student_name " +
            "FROM comments c " +
            "LEFT JOIN users s ON c.user_id = s.id " +
            "LEFT JOIN comments p ON c.parent_comment_id = p.id " +
            "LEFT JOIN users ps ON p.user_id = ps.id " +
            "WHERE c.id = #{id}")
    @Results(id = "CommentResultMap", value = {
        @Result(property = "id", column = "comment_id", id = true),
        @Result(property = "createTime", column = "comment_create_time"),
        @Result(property = "updateTime", column = "comment_update_time"),
        @Result(property = "content", column = "comment_content"),
        @Result(property = "student.id", column = "student_id"),
        @Result(property = "student.username", column = "student_name"),
        @Result(property = "goods.id", column = "goods_id"),
        @Result(property = "parent.id", column = "parent_id"),
        @Result(property = "parent.student.id", column = "parent_student_id"),
        @Result(property = "parent.student.username", column = "parent_student_name")
    })
    Comment findById(Long id);

    /**
     * 根据商品 ID 查询评论列表。
     * @param goodsId 商品 ID。
     * @return 对应商品的评论列表。
     */
    @Select("SELECT c.id AS comment_id, c.created_at AS comment_create_time, c.updated_at AS comment_update_time, " +
            "c.content AS comment_content, c.product_id AS goods_id, c.parent_comment_id AS parent_id, " +
            "s.id AS student_id, s.username AS student_name, " +
            "ps.id AS parent_student_id, ps.username AS parent_student_name " +
            "FROM comments c " +
            "LEFT JOIN users s ON c.user_id = s.id " +
            "LEFT JOIN comments p ON c.parent_comment_id = p.id " +
            "LEFT JOIN users ps ON p.user_id = ps.id " +
            "WHERE c.product_id = #{goodsId} " +
            "ORDER BY c.created_at ASC")
    @ResultMap("CommentResultMap")
    List<Comment> findByGoodsId(@Param("goodsId") Long goodsId);

    /**
     * 保存评论。
     *
     * @param comment 要保存的评论对象。
     * @return 受影响的行数。
     */
    @Insert("INSERT INTO comments (user_id, product_id, content, parent_comment_id, created_at, updated_at) " +
            "VALUES (#{student.id}, #{goods.id}, #{content}, #{parent.id}, NOW(), NOW())")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int save(Comment comment);

    /**
     * 查询所有评论。
     */
    @Select("SELECT c.id AS comment_id, c.created_at AS comment_create_time, c.updated_at AS comment_update_time, " +
            "c.content AS comment_content, c.product_id AS goods_id, c.parent_comment_id AS parent_id, " +
            "s.id AS student_id, s.username AS student_name, " +
            "ps.id AS parent_student_id, ps.username AS parent_student_name " +
            "FROM comments c " +
            "LEFT JOIN users s ON c.user_id = s.id " +
            "LEFT JOIN comments p ON c.parent_comment_id = p.id " +
            "LEFT JOIN users ps ON p.user_id = ps.id " +
            "ORDER BY c.created_at DESC")
    @ResultMap("CommentResultMap")
    List<Comment> findAll();

    /**
     * 查询所有被举报的评论。
     */
    @Select("SELECT c.id AS comment_id, c.created_at AS comment_create_time, c.updated_at AS comment_update_time, " +
            "c.content AS comment_content, c.product_id AS goods_id, c.parent_comment_id AS parent_id, " +
            "c.is_reported, " +
            "s.id AS student_id, s.username AS student_name, " +
            "ps.id AS parent_student_id, ps.username AS parent_student_name " +
            "FROM comments c " +
            "LEFT JOIN users s ON c.user_id = s.id " +
            "LEFT JOIN comments p ON c.parent_comment_id = p.id " +
            "LEFT JOIN users ps ON p.user_id = ps.id " +
            "WHERE c.is_reported = TRUE " +
            "ORDER BY c.created_at DESC")
    @ResultMap("CommentResultMap")
    List<Comment> findReported();

    /**
     * 根据评论 ID 删除评论。
     */
    @Delete("DELETE FROM comments WHERE id = #{id}")
    int deleteById(Long id);


}